package com.logicbus.xscript;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.Settings;
import com.logicbus.backend.ServantFactory;
import com.logicbus.backend.ServantRegistry;
import com.logicbus.models.catalog.CatalogNode;
import com.logicbus.models.servant.ServantCatalog;
import com.logicbus.models.servant.ServantCatalogNode;
import com.logicbus.models.servant.ServiceDescription;
import org.apache.commons.lang3.StringUtils;

/**
 * 遍历所有的服务描述
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SvcScan extends Segment {
    protected boolean doSwagger = false;

    public SvcScan(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        doSwagger = PropertiesConstants.getBoolean(p,"swagger",doSwagger);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        Settings settings = Settings.get();
        ServantFactory sf = (ServantFactory)settings.get("servantFactory");
        ServantRegistry sm = sf.getServantRegistry();
        ServantCatalog[] catalog = sm.getServantCatalog();
        for (int i = 0 ; i < catalog.length ; i ++){
            ServantCatalogNode node = (ServantCatalogNode) catalog[i].getRoot();
            if (node != null){
                scanCatalog(catalog[i],node,root,current,ctx,watcher);
            }
        }
    }

    protected void scanCatalog(ServantCatalog catalog,ServantCatalogNode catalogRoot,XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher){
        ServiceDescription[] services = catalogRoot.getServices();

        for (ServiceDescription sd:services){
            LogicletContext childCtx = new LogicletContext(ctx);

            childCtx.SetValue("$id",sd.getServiceID());
            childCtx.SetValue("$name",sd.getName());
            childCtx.SetValue("$note",sd.getNote());
            childCtx.SetValue("$module",sd.getModule());
            childCtx.SetValue("$visible",sd.getVisible());
            childCtx.SetValue("$path",sd.getPath());
            childCtx.SetValue("$log",sd.getLogType().toString());
            childCtx.SetValue("$acGroup",sd.getAcGroup());
            childCtx.SetValue("$privilege", StringUtils.isEmpty(sd.getPrivilege())?sd.getPath():sd.getPrivilege());
            childCtx.SetValue("$desc",
                    String.format("%s\n\n**Module**:<code>%s</code>\n\n**Visible**:<code>%s</code>\n\n**Log Type**:<code>%s</code>\n\n**Access Control Group**:<code>%s</code>\n\n**Privilage**:<code>%s</code>",
                            sd.getNote(),sd.getModule(),sd.getVisible(),sd.getLogType().toString(),sd.getAcGroup(),sd.getPrivilege()));

            String swagger = "false";
            if (doSwagger){
                Logiclet onSwagger = sd.getSwaggerLogiclet();
                if (onSwagger != null){
                    onSwagger.execute(root,current,childCtx,watcher);
                    swagger = "true";
                }
            }
            childCtx.SetValue("$swagger",swagger);
            super.onExecute(root, current, childCtx, watcher);
        }

        //迭代子节点
        CatalogNode[] children = catalog.getChildren(catalogRoot);
        if (children == null || children.length <= 0)
            return ;

        for (int i = 0 ; i < children.length ; i ++){
            scanCatalog(catalog,(ServantCatalogNode)children[i],root,current,ctx,watcher);
        }
    }
}
